/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.dao.cd.impl;

import java.awt.Color;

import org.jdom.Attribute;
import org.jdom.Element;

import who.tmap.bo.BizObject;
import who.tmap.bo.cd.CDDotDensity;
import who.tmap.bo.cd.DotDensityDistributions;
import who.tmap.dao.TMapDAOException;

public class CDRandomDotDensityDAOImpl extends BaseCartographicDisplayDAOImpl {

	CDRandomDotDensityDAOImpl() {
		super();
	}
	
	public BizObject loadXml(Element elt) throws TMapDAOException {
		CDDotDensity cd =null;
	
		try {
			cd =(CDDotDensity)getCD( elt );
			Element mainElt =elt.getChild( "RandomDotDensity" );
			
			cd.setJoinIdColumnName( getNullStringIfEmpty( mainElt.getAttributeValue( "joinIdColumnName" ) ) );
			cd.setDataColumnName( getNullStringIfEmpty( mainElt.getAttributeValue( "dataColumnName" ) ) );
			
			Attribute distrib =mainElt.getAttribute("distribution");
			if ( distrib != null ) {
				cd.setDistribution( DotDensityDistributions.Distribs.fromName( distrib.getValue() ) );
			} else {
				cd.setDistribution( DotDensityDistributions.Distribs.DEFAULT );
			}
			
			cd.setNbUnitsPerSymbol( mainElt.getAttribute( "nbUnitsPerSymbols" ).getIntValue() );			
			cd.setPointSize( mainElt.getAttribute( "pointSize" ).getIntValue() );
			
			cd.setColor( miscHelper.rgbToColor( mainElt.getAttributeValue( "color") ) );
			
		} catch( Exception e ) {
			throw new TMapDAOException( e );
		}
		
		
		return cd;
	}

	public Element saveXml(BizObject object) throws TMapDAOException {
		CDDotDensity cd =(CDDotDensity)object;
		
        Element rootElt =super.saveXml( object );
        Element mainElt =new Element( "RandomDotDensity" );
        rootElt.addContent( mainElt );
        
        mainElt.setAttribute( "version", VERSION );
        
        mainElt.setAttribute( "joinIdColumnName", getNonNullString( cd.getJoinIdColumnName() ) );
        mainElt.setAttribute( "dataColumnName", getNonNullString( cd.getDataColumnName() ) );
        mainElt.setAttribute( "distribution", cd.getDistribution().getName() );
        mainElt.setAttribute( "nbUnitsPerSymbols", Integer.toString( cd.getNbUnitsPerSymbol() ) );
        mainElt.setAttribute( "pointSize", Integer.toString( cd.getPointSize() ) );
        
        mainElt.setAttribute( "color", miscHelper.colorToHexa( cd.getColor() != null 
        															? cd.getColor() 
        															: Color.BLACK ) );       
        
		return rootElt;
	}
	


}
